
setwd("/Users/sungheekim02/Documents/Project/decoy/analysis")

d <- read.csv("pilot_study_results.csv", head=TRUE, sep=",")
preference.difference=d$preference_target-d$preference_competitor
d=cbind(d,preference.difference)

# d <- subset(d, d$purchase == '1')
d.tableNC<-subset(d,d$interface_type=='table')

#d.table.control <- subset(d.table, d.table$setting == 'control')
#d.table.decoy <- subset(d.table, d.table$setting == 'decoy')

# To use this, you need the ggplot2 packages
# TODO: add label for count
control_hist <- 
  ggplot(d.control, aes(x=selection)) + geom_histogram(binwidth=1, stat = "bin", position = 'identity') +
  xlab("Selection") + ylab("Frequency") +
  labs(title = "Histogram of Selection on Control Setting") + scale_x_discrete(limits=c("competitor", "target")) + scale_y_continuous(limits=c(0,50))

decoy_hist <- 
  ggplot(d.decoy, aes(x=selection)) + geom_histogram(binwidth=1, stat = "bin", position = 'identity') +
  xlab("Selection") + ylab("Frequency") +
  labs(title = "Histogram of Selection on Decoy Setting") + scale_x_discrete(limits=c("competitor", "target", "decoy")) + scale_y_continuous(limits=c(0,50))

# create a table by counting the entities
selection.tableNC=table(d.tableNC$selection,d.tableNC$setting)

# 1. Chi-Squared test
selection.tableNC
chisq.test(selection.tableNC)
fisher.test(selection.tableNC)

# 2. Kruskall-Wallis test
kruskal.test(preference_target ~ setting, data = d.tableNC)
kruskal.test(preference_competitor ~ setting, data = d.tableNC)
kruskal.test(preference.difference ~ setting, data = d.tableNC)

##############################################################

d.tableC<-subset(d,d$interface_type=='table_C')
selection.tableC=table(d.tableC$selection,d.tableC$setting)
selection.tableC
chisq.test(selection.tableC)
fisher.test(selection.tableC)
kruskal.test(preference_target ~ setting, data = d.tableC)
kruskal.test(preference_competitor ~ setting, data = d.tableC)
kruskal.test(preference.difference ~ setting, data = d.tableC)

##############################################################

d.PC<-subset(d,d$interface_type=='parallel_coordinate')
selection.PC=table(d.PC$selection,d.PC$setting)
selection.PC
chisq.test(selection.PC)
fisher.test(selection.PC)
kruskal.test(preference_target ~ setting, data = d.PC)
kruskal.test(preference_competitor ~ setting, data = d.PC)
kruskal.test(preference.difference ~ setting, data = d.PC)

##############################################################

d.TSB<-subset(d,d$interface_type=='two_sided_bars')
selection.TSB=table(d.TSB$selection,d.TSB$setting)
selection.TSB
chisq.test(selection.TSB)
fisher.test(selection.TSB)
kruskal.test(preference_target ~ setting, data = d.TSB)
kruskal.test(preference_competitor ~ setting, data = d.TSB)
kruskal.test(preference.difference ~ setting, data = d.TSB)

##############################################################

d.stackBar<-subset(d,d$interface_type=='stacked_bars')
selection.stackBar=table(d.stackBar$selection,d.stackBar$setting)
selection.stackBar
chisq.test(selection.stackBar)
fisher.test(selection.stackBar)
kruskal.test(preference_target ~ setting, data = d.stackBar)
kruskal.test(preference_competitor ~ setting, data = d.stackBar)
kruskal.test(preference.difference ~ setting, data = d.stackBar)